package com.lagou.task10;

import com.sun.source.tree.CompilationUnitTree;

import java.util.Scanner;

/**
 * 2. 编程实现控制台版并支持两人对战的五子棋游戏。
 * （1）绘制棋盘 - 写一个成员方法实现
 * （2）提示黑方和白方分别下棋并重新绘制棋盘 - 写一个成员方法实现。
 * （3）每当一方下棋后判断是否获胜 - 写一个成员方法实现。
 */



public class Homework2 {
    //定义一个二维数组作为棋盘
    static char[][] arr = new char[16][16];
    //使用成员方法将棋盘初始化
    public static void init() {
        for (int i=0; i<arr.length; i++){
            for(int j=0; j<arr.length; j++) {
                arr[i][j] = '+';  //没有下棋的位置
            }
            System.out.println();
        }
        draw();
    }

    //绘制棋盘的成员方法
    private static void draw() {
        for(int i=0; i<arr.length; i++) {
            for(int j=0; j<arr.length; j++){
                System.out.println(arr[i][j] + " ");
            }
            System.out.println();
        }
    }

    //下棋的方法, 控制台输入下棋位置, 并判断该处有没有棋子, 且是否在期盼范围内
    public static void play(){
        while(true){
            Scanner sc = new Scanner(System.in);
            //黑方先操作
            System.out.println("黑方下子ing...");
            System.out.println("黑子想下载第几行:");
            int x1 = sc.nextInt();
            System.out.println("黑子想下在第几列:");
            int y1 = sc.nextInt();
            while(true) {
                if(x1>15 || y1>15 || x1<0 || y1<0) {
                    System.out.println("该位置不再期盼范围, 请重新下子");
                    System.out.println("黑方下子ing...");
                    System.out.println("黑方想下载第几行: ");
                    x1 = sc.nextInt();
                    System.out.println("黑方想下载第几列:");
                    y1 = sc.nextInt();
                } else if(arr[x1][y1] != '+') {
                    System.out.println("该位置已有棋子,请重新下");
                    System.out.println("黑方下子ing...");
                    System.out.println("黑方想下载第几行: ");
                    x1 = sc.nextInt();
                    System.out.println("黑方想下载第几列:");
                    y1 = sc.nextInt();
                } else {
                    break;
                }
            }

            arr[x1][y1] = '0'; //黑子为0
            draw();
            if(ifWin(x1, y1)) {
                System.out.println("黑子胜利啦!!");
                break;
            }

            //白子操作
            System.out.println("白方下子ing....");
            System.out.println("白子想下在第几行：");
            int x2 = sc.nextInt();
            System.out.println("白子想下在第几列：");
            int y2 = sc.nextInt();

            while(true) {
                if(x2>15 || y2 > 15 || x2 <0 || y2<0) {
                    System.out.println("不在棋盘内，请重新下子");
                    System.out.println("白方下子ing....");
                    System.out.println("白子想下在第几行：");
                    x2 = sc.nextInt();
                    System.out.println("白子想下在第几列：");
                    y2 = sc.nextInt();
                } else if (arr[x2][y2] != '+') {
                    System.out.println("此处有子，请重新下子");
                    System.out.println("白方下子ing....");
                    System.out.println("白子想下在第几行：");
                    x2 = sc.nextInt();
                    System.out.println("白子想下在第几列：");
                    y2 = sc.nextInt();
                } else {
                    break;
                }
            }
            arr[x2][y2] ='1'; //白子为1
            draw();
            if(ifWin(x2,y2)) {
                System.out.println("白子胜利啦!!!");
                break;
            }
        }
    }

    //判断该遍历行, 列, 左斜, 右斜 如果遇到'0' 黑子计数+1, 白子计数清零
    //如果遇到1, 白子计数+1 黑子计数清零; 如果遇到+ 两者都清零
    public static boolean ifWin(int a, int b) {
        boolean flag = false;
        int countWhite = 0; //白子计数
        int countBlack = 0; //黑子计数
        //上下方向
        for (int i = a - 4; i <= a + 4; i++) {
            if (i < 16 && i >= 0) {
                if (arr[i][b] == '0') {
                    countBlack++;
                    countWhite = 0;
                    if (countBlack == 5) {
                        return flag = true;
                    }
                } else if (arr[i][b] == '1') {
                    countWhite++;
                    countBlack = 0;
                    if (countWhite == 5) {
                        return flag = true;
                    }
                } else if (arr[i][b] == '+') {
                    countWhite = 0;
                    countBlack = 0;
                }
            }
        }
        countWhite = 0;
        countBlack = 0;
        System.out.println(countBlack + " " + countWhite);
        System.out.println();

        //左右方向
        for (int i = b - 4; i <= b + 4; i++) {
            if (i < 16 && i >= 0) {
                if (arr[a][i] == '0') {
                    countBlack++;
                    countWhite = 0;
                    if (countBlack == 5) {
                        return flag = true;
                    }
                } else if (arr[a][i] == '1') {
                    countWhite++;
                    countBlack = 0;
                    if (countWhite == 5) {
                        return flag = true;
                    }
                }else if(arr[a][i] == '+'){
                    countWhite = 0;
                    countBlack = 0;
                }
            }
        }

        //左上到右下
        // 左上到右下
        for (int i = -4; i <= 4; i++) {
            if (a + i < 16 && b + i < 16 && a + i >= 0 && b + i >= 0) {//
                if (arr[a + i][b + i] == '0') {
                    countBlack++;
                    countWhite = 0;
                    if (countBlack == 5) {
                        return flag = true;
                    }
                } else if (arr[a + i][b + i] == '1') {
                    countWhite ++;
                    countBlack  = 0;
                    if (countWhite  == 5) {
                        return flag = true;
                    }
                } else if (arr[a + i][b + i]=='+'){
                    countWhite = 0;
                    countBlack = 0;
                }
            }
        }

        countWhite = 0;
        countBlack = 0;
        System.out.println(countBlack+" "+countWhite );

        //右上到左下
        for (int i = -4; i <= 4; i++) {
            if (a - i < 16 && b + i < 16 && a - i >= 0 && b + i >= 0) {
                if (arr[a - i][b + i] == '0') {
                    countBlack++;
                    countWhite = 0;
                    if (countBlack == 5) {
                        return flag = true;
                    }
                } else if (arr[a - i][b + i] == '1') {
                    countWhite++;
                    countBlack = 0;
                    if (countWhite == 5) {
                        return flag = true;
                    }
                } else if (arr[a - i][b + i]=='+'){
                    countWhite = 0;
                    countBlack = 0;
                }
            }
        }
        System.out.println(countBlack+" "+countWhite);
        return flag;
    }

    public static void main(String[] args) {
        init();//初始化
        play();//开始游戏
    }
}
